package com.hfzy.ihk.web.freeswitchManage.controller;


import com.alibaba.fastjson.JSON;
import com.hfzy.ihk.common.constants.GlobalStatic;
import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.common.web.WebResult;
import com.hfzy.ihk.facade.freeswitchManage.entity.Userinfo;
import com.hfzy.ihk.facade.freeswitchManage.request.QueryUserinfosRequest;
import com.hfzy.ihk.facade.freeswitchManage.response.QueryUserinfosResponse;
import com.hfzy.ihk.facade.freeswitchManage.service.IUserinfoService;
import com.hfzy.ihk.facade.freeswitchManage.vo.UserinfoVo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wws
 * @since 2018-12-29
 */
@RestController
@RequestMapping("/userinfo")
@Log4j
public class UserinfoController {

    @Autowired
    IUserinfoService userinfoService;

    @RequestMapping(value = "queryUserinfo")
    public ModelAndView queryUserinfo(HttpServletRequest request, HttpServletResponse response, UserinfoVo userinfoVo) {

        ModelAndView model = new ModelAndView();
        model.addObject("userinfoVo", userinfoVo);
        model.setViewName("page/userinfo/queryUserinfo");
        return model;
    }

    @RequestMapping("/queryUserinfoList")
    public WebResult queryUserinfoList(@RequestParam Map<String, String> params){

        long start = System.currentTimeMillis();
        WebResult result = new WebResult();
        try {

            QueryUserinfosRequest request = new QueryUserinfosRequest();

            if(StringTools.isNotempty(params.get("username"))){
                request.setUsername(params.get("username").trim());
            }

            if(StringTools.isNotempty(params.get("name"))){
                request.setName(params.get("name").trim());
            }

            if(StringTools.isNotempty(params.get("enrollNumber"))){
                request.setEnrollNumber(params.get("enrollNumber").trim());
            }

            if(StringTools.isNotempty(params.get("pageSize"))){
                request.setPageSize(Long.parseLong(params.get("pageSize").trim()));
            }

            if(StringTools.isNotempty(params.get("page"))){
                request.setPage(Long.parseLong(params.get("page").trim()));
            }

            QueryUserinfosResponse response = userinfoService.queryUserinfos(request);


            result.setTotal(response.getTotal());
            result.setCode(GlobalStatic.CODE_1);
            result.setMsg(GlobalStatic.SUCCESS_CHAR);
            result.setRecords(response.getDatas());
        } catch (Exception e) {
            log.error("查询用户列表异常，",e);
            result.setCode(GlobalStatic.CODE_N);
            result.setMsg(GlobalStatic.FAIL_CHAR);
        }
        log.info("客户查询总耗时："+(System.currentTimeMillis()-start));
        return result;
    }

    @RequestMapping("/queryUserinfoById")
    public WebResult queryUserinfoById( int userinfoId){

        long start = System.currentTimeMillis();
        WebResult result = new WebResult();
        try {


            Userinfo userinfo = userinfoService.queryUserinfoById(userinfoId);

            List<Userinfo> userinfoList = new ArrayList<Userinfo>();
            if(userinfo != null) {
                result.setTotal(1);
                userinfoList.add(userinfo);
            }
            result.setCode(GlobalStatic.CODE_1);
            result.setMsg(GlobalStatic.SUCCESS_CHAR);
            result.setRecords(userinfoList);
        } catch (Exception e) {
            log.error("查询用户列表异常，",e);
            result.setCode(GlobalStatic.CODE_N);
            result.setMsg(GlobalStatic.FAIL_CHAR);
        }
        log.info("客户查询总耗时："+(System.currentTimeMillis()-start));
        return result;
    }

    @RequestMapping(value = "editUserinfo")
    public ModelAndView editUserinfo(HttpServletRequest request, HttpServletResponse response,Integer userinfoId) {

        Userinfo userinfo = null;
        ModelAndView model = new ModelAndView();
        if(userinfoId == null || userinfoId <= 0){
            userinfo = new Userinfo();
        }else {
            userinfo = userinfoService.queryUserinfoById(userinfoId);
            if(userinfo == null){
                userinfo = new Userinfo();
            }
        }

        model.addObject("data", userinfo);
        model.setViewName("page/userinfo/editUserInfo.html");
        return model;
    }


    @RequestMapping("/updateUserinfo")
    public WebResult updateUserinfoById(Userinfo userinfo){

        long start = System.currentTimeMillis();
        WebResult result = new WebResult();
        try {

            if (userinfo.getId() == null || userinfo.getId() <= 0){
                userinfoService.save(userinfo);
            }else {
                userinfoService.updateById(userinfo);
            }

            result.setCode(GlobalStatic.CODE_1);
            result.setMsg(GlobalStatic.SUCCESS_CHAR);
        } catch (Exception e) {
            log.error("updateUserinfo异常，",e);
            result.setCode(GlobalStatic.CODE_N);
            result.setMsg(GlobalStatic.FAIL_CHAR);
        }
        log.info("updateUserinfo总耗时："+(System.currentTimeMillis()-start));
        return result;
    }

    @RequestMapping("/delUserinfoById")
    public WebResult delUserinfoById(Integer userinfoId){

        long start = System.currentTimeMillis();
        WebResult result = new WebResult();
        try {

            if(userinfoId != null && userinfoId > 0){
                userinfoService.removeById(userinfoId);
            }

            result.setCode(GlobalStatic.CODE_1);
            result.setMsg(GlobalStatic.SUCCESS_CHAR);
        } catch (Exception e) {
            log.error("delUserinfoById异常，",e);
            result.setCode(GlobalStatic.CODE_N);
            result.setMsg(GlobalStatic.FAIL_CHAR);
        }
        log.info("delUserinfoById总耗时："+(System.currentTimeMillis()-start));
        return result;
    }

    @RequestMapping("/delUserinfoByIds")
    public WebResult delUserinfoByIds(String ids){

        long start = System.currentTimeMillis();
        WebResult result = new WebResult();
        try {

            if(StringTools.isNotempty(ids)){
                userinfoService.removeByIds(Arrays.asList(ids.split(",")));
            }

            result.setCode(GlobalStatic.CODE_1);
            result.setMsg(GlobalStatic.SUCCESS_CHAR);
        } catch (Exception e) {
            log.error("delUserinfoByIds异常，",e);
            result.setCode(GlobalStatic.CODE_N);
            result.setMsg(GlobalStatic.FAIL_CHAR);
        }
        log.info("delUserinfoByIds总耗时："+(System.currentTimeMillis()-start));
        return result;
    }
}
